package com.arrogames.blondbattle.game.graphics.ui 
{
	import com.arrogames.blondbattle.assets.AssetManager;
	import com.arrogames.blondbattle.game.purchases.events.PurchaseEvent;
	import com.arrogames.blondbattle.model.UnitDefinition;
	import feathers.controls.Button;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import starling.textures.Texture;
	import flash.geom.Matrix;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import com.greensock.TweenLite;
	import starling.textures.TextureAtlas;
	
	/**
	 * ...
	 * @author Robin van den Bogaard
	 */
	public class PurchaseButton extends Sprite
	{
		
		private var unit:UnitDefinition;
		private var buttonDisplay:Button;
		private var cooldownDisplay:Image;
		
		public function PurchaseButton(unit:UnitDefinition, buttonSize:Number)
		{
			super();
			
			var iconTexture:Texture = AssetManager.instance.getTexture(unit.name.toLowerCase() + "_east_1"); 
			var unitImage:Image = new Image(iconTexture);
			unitImage.width = Math.min(46, iconTexture.width);
			unitImage.height = Math.min(46, iconTexture.height);
			this.unit = unit;
			
			this.buttonDisplay = new Button();
			this.buttonDisplay.defaultSkin = new Image(AssetManager.instance.getTexture("UnitButtonBg"));
			this.buttonDisplay.defaultIcon = unitImage;
			this.buttonDisplay.iconPosition = Button.ICON_POSITION_TOP;
			this.buttonDisplay.width = buttonSize;
			this.buttonDisplay.height = buttonSize;
			this.buttonDisplay.label = unit.name +": " + unit.costs +"$";
			this.buttonDisplay.addEventListener(Event.TRIGGERED, onTriggered);
			this.addChild(buttonDisplay);
			
			drawCooldownDisplay(buttonSize);
		}
		
		public function startCooldownAnimation() : void 
		{
			buttonDisplay.isEnabled = false;
			addChild(cooldownDisplay);
			cooldownDisplay.alpha = 0.5;
			TweenLite.to( cooldownDisplay, unit.cooldown, { alpha:0, onComplete:onTweenComplete } );
		}
		
		override public function dispose():void
		{
			super.dispose();
			this.buttonDisplay.removeEventListener(Event.TRIGGERED, onTriggered);
			this.removeChild(buttonDisplay);
			this.unit = null;
		}
		
		private function onTriggered(e:Event):void
		{
			dispatchEvent(new PurchaseEvent(unit));
		}
		
		private function onTweenComplete() : void 
		{
			buttonDisplay.isEnabled = true;
			removeChild(cooldownDisplay);
		}
		
		private function drawCooldownDisplay(buttonSize:Number) : void
		{
			var shape:Shape = new Shape();
			shape.graphics.beginFill(0x000000);
			shape.graphics.moveTo(0, 0);
			shape.graphics.lineTo(buttonSize, 0);
			shape.graphics.lineTo(buttonSize, buttonSize);
			shape.graphics.lineTo(0, buttonSize);
			shape.graphics.lineTo(0, 0);
			shape.graphics.endFill();
			var bitmapData : BitmapData = new BitmapData( buttonSize, buttonSize, true, 0 );
			bitmapData.draw( shape );
			var texture : Texture = Texture.fromBitmapData( bitmapData, false, false, 1 );
			
			cooldownDisplay = new Image(texture);
			
		}
	}
}